﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace NSATBPortal
{
    public partial class DefRunEditItems : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string sVal = Server.UrlDecode(Request.QueryString["Name"].ToString());
            lblParent.Text = "Items for " + sVal;
        }

        protected void DefRunEditItemsGrid_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int iIndex = e.NewEditIndex;

            string sRecID = DefRunEditItemsGrid.Rows[iIndex].Cells[0].Text;
            string sRecName = DefRunEditItemsGrid.Rows[iIndex].Cells[1].Text;
            string sRecValue = DefRunEditItemsGrid.Rows[iIndex].Cells[2].Text;
            if (sRecValue == "&nbsp;" || sRecValue.Trim().Length == 0)
                sRecValue = "NULL";

            string sParentID = Request.QueryString["ID"].ToString();
            string sParentName = Request.QueryString["Name"].ToString();

            Response.Redirect("DefRunItemDetail.aspx?Op=Edit&ID=" + sRecID.Trim() + "&ParentID=" + sParentID + "&ParentName=" + sParentName);
        }

        protected void DefRunEditItemsGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int iIndex = e.RowIndex;

            int iItemID = Convert.ToInt32(DefRunEditItemsGrid.Rows[iIndex].Cells[0].Text);

            string sUserID = Request.LogonUserIdentity.Name;
            using (ATBSchemaDB objATBDB = new ATBSchemaDB())
            {
                objATBDB.DefRunItemRemoveItem(iItemID, sUserID);
            }

            // reload page
            Response.Redirect(Request.RawUrl);
        }

        protected void DefRunEditItemsGrid_Init(object sender, EventArgs e)
        {
            string sVal = Request.QueryString["ID"].ToString();
            int iParentID = Convert.ToInt32(sVal);

            DataTable dtResults;
            using (ATBSchemaDB objATBDB = new ATBSchemaDB())
            {
                dtResults = objATBDB.GetDefRunItemList(iParentID);
            }

            DefRunEditItemsGrid.DataSource = dtResults;
            DefRunEditItemsGrid.DataBind();
        }

        protected void ActionMenu_MenuItemClick(object sender, MenuEventArgs e)
        {
            if (e.Item.Text == "Add Item")
            {
                string sVal = Request.QueryString["ID"].ToString();
                int iParentID = Convert.ToInt32(sVal);
                string sParentName = Request.QueryString["Name"].ToString();

                Response.Redirect("DefRunItemDetail.aspx?Op=Add&ParentID=" + iParentID.ToString() + "&ParentName=" + sParentName);
            }
        }
    }
}